package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.provider.dao.OmcStatisticsMapper;
import com.csun.cmny.provider.dto.MonthlyCountQueryDto;
import com.csun.cmny.provider.model.domain.OmcUserType;
import com.csun.cmny.provider.model.vo.OldSelfDataNewVo;
import com.csun.cmny.provider.model.vo.OldTypeDataVo;
import com.csun.cmny.provider.service.OmcUserTypeService;
import com.csun.cmny.provider.service.StatisticsService;
import com.csun.cmny.provider.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chenzheng
 * @description:统计分析服务层
 * @date 2019/9/4 11:12
 */
@Slf4j
@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Resource
    private OmcStatisticsMapper statisticsMapper;

    @Resource
    private OmcUserTypeService userTypeService;

    /**
     * 通过年 月获取当前月份老人人数
     * @param year 年
     * @param month 月
     * @param groupId 机构ID
     * @return
     */
    @Override
    public Integer getOldmanCountByYearAndMonth(Integer year, Integer month, Long groupId) {
        return statisticsMapper.getOldmanCountByYearAndMonth(year, month, groupId);
    }

    /**
     * 通过年 月获取当前月份之前老人总数
     * @param year 年
     * @param month 月
     * @param groupId 机构ID
     * @return
     */
    @Override
    public Integer getAllOldmanCountByYearAndMonth(Integer year, Integer month, Long groupId) {
        return statisticsMapper.getAllOldmanCountByYearAndMonth(year, month, groupId);
    }

    /**
     * 通过年 月获取当前月份去世总人数
     * @param year 年
     * @param month 月
     * @param groupId 机构ID
     * @return
     */
    @Override
    public Integer getDeadOldmanCountByYearAndCount(Integer year, Integer month, Long groupId) {
        return statisticsMapper.getDeadOldmanCountByYearAndCount(year, month, groupId);
    }

    /**
     * 通过年 月 楼栋id 房间id 获取当月入住的床位数
     * @param year 年
     * @param month 月
     * @param buildingId 楼楼栋D
     * @param floorNo 楼层
     * @return
     */
    @Override
    public Integer getBedCountByBuildingIdAndFloorNo(Integer year, Integer month, Integer buildingId, Integer floorNo) {
        return statisticsMapper.getBedCountByBuildingIdAndFloorNo(year, month, buildingId, floorNo);
    }

    /**
     * 获取当前楼栋的层数
     * @param buildingId 楼栋ID
     * @return
     */
    @Override
    public List<Integer> getAllFloorNoByBuildingId(Integer buildingId) {
        return statisticsMapper.getAllFloorNoByBuildingId(buildingId);
    }

    /**
     * 查询机构月份入院老人
     * @param groupId
     * @param date
     * @return
     */
    @Override
    public Integer selectByDate(Long groupId, String date) {
        return statisticsMapper.selectByDate(groupId, date);
    }

    /**
     * 老人年龄统计
     * @param groupId
     * @return
     */
    @Override
    public List<Integer> oldAgeStatistics(Long groupId) {
        return statisticsMapper.oldAgeStatistics(groupId);
    }

    /**
     * 机构老人按人员类别统计人数
     * @param groupId
     * @return
     */
    @Override
    public List<OldTypeDataVo> oldTypeStatistics(Long groupId) {
        List<OldTypeDataVo> list = new ArrayList<>();
        //查询出机构的人员类别
        OmcUserType omcUserType =new OmcUserType();
        omcUserType.setGroupId(groupId);
        List<OmcUserType> userTypeList = userTypeService.select(omcUserType);
        if(null != userTypeList && userTypeList.size() > 0){
            //统计人数
            for (OmcUserType userType: userTypeList) {
                OldTypeDataVo typeDataVo = new OldTypeDataVo();
                typeDataVo.setUserTypeId(userType.getId());//人员类别ID
                typeDataVo.setUserTypeName(userType.getUserTypeName());//人员类别名
                typeDataVo.setOldCount(statisticsMapper.oldTypeStatistics(groupId,userType.getId()));//该人员类别老人数量
                list.add(typeDataVo);
            }
        }
        return list;
    }

    /**
     * 老人类型统计 -- 失能状态统计
     *
     * @param groupId
     * @return
     */
    @Override
    public OldSelfDataVo oldManTypeStatistics(Long groupId) {
        return statisticsMapper.oldManTypeStatistics(groupId);
    }

    /**
     * 老人性别统计
     * @param groupId
     * @return
     */
    @Override
    public OldSexDataVo oldManSexStatistics(Long groupId) {
        return statisticsMapper.oldManSexStatistics(groupId);
    }

    /**
     * 查询月份入院老人汇总
     * @param monthlyCountQueryDto
     * @return
     */
    @Override
    public PageInfo<OldManSummaryVo> selectByInOldMan(MonthlyCountQueryDto monthlyCountQueryDto) {
        PageHelper.startPage(monthlyCountQueryDto.getPageNum(), monthlyCountQueryDto.getPageSize());
        List<OldManSummaryVo> giftVoList = statisticsMapper.selectByInOldMan(monthlyCountQueryDto);
        return new PageInfo<>(giftVoList);
    }

    /**
     * 查询月份出院老人汇总
     * @param monthlyCountQueryDto
     * @return
     */
    @Override
    public PageInfo<OldManSummaryVo> selectByOutOldMan(MonthlyCountQueryDto monthlyCountQueryDto) {
        PageHelper.startPage(monthlyCountQueryDto.getPageNum(), monthlyCountQueryDto.getPageSize());
        List<OldManSummaryVo> giftVoList = statisticsMapper.selectByOutOldMan(monthlyCountQueryDto);
        return new PageInfo<>(giftVoList);
    }

    /**
     * 查询月份调房调级老人汇总
     * @param monthlyCountQueryDto
     * @return
     */
    @Override
    public PageInfo<OldManSummaryVo> selectByUpdateOldMan(MonthlyCountQueryDto monthlyCountQueryDto) {
       PageHelper.startPage(monthlyCountQueryDto.getPageNum(), monthlyCountQueryDto.getPageSize());
       List<OldManSummaryVo> giftVoList = statisticsMapper.selectByUpdateOldMan(monthlyCountQueryDto);
       return new PageInfo<>(giftVoList);
    }

    /**
     * 通过年 月获取每月入院,出院,调房调级人数
     * @return
     */
    @Override
    public OldManMonthCountVo selectByMonthCount(Map map){
        return statisticsMapper.selectByMonthCount(map);
    }

    @Override
    public List<OldManVo> listOldmanByBirthMonth(String month, LoginAuthDto loginAuthDto) {
        return statisticsMapper.listOldmanByBirthMonth(month,loginAuthDto.getGroupId());
    }

    @Override
    public List<OldSelfDataNewVo> newOldManTypeStatistics(Long groupId) {
        return statisticsMapper.newOldManTypeStatistics(groupId);
    }


}
